class Solution:
    def maxTaxiEarnings(self, n: int, rides: list) -> int:
        dp = [0 for _ in range(n+1)]
        earnTableList = [[] for _ in range(n+1)]
        for i in range(len(rides)):
            ride = rides[i]
            earnTableList[ride[1]].append(ride)
        res = 0
        for i in range(1, n+1):
            maxEarn = dp[i-1]
            for earnTable in earnTableList[i]:
                earn = earnTable[1] - earnTable[0] + \
                    earnTable[2] + dp[earnTable[0]]
                maxEarn = max(maxEarn, earn)
            dp[i] = maxEarn
            res = max(res, dp[i])

        return res
